package org.Servlet;
import java.io.IOException;
import org.Dao.LoginDao;
import org.model.Login;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,
			IOException {
		request.setCharacterEncoding("gb2312");
		//该类为项目与数据的接口（DAO接口），用于处理数据与数据库表的一些操作
		LoginDao loginDao = new LoginDao();
		Login l = loginDao.checkLogin(request.getParameter("name"), request.getParameter("password"));
		if(l!=null){												//如果登录成功
			HttpSession session = request.getSession();	//获得会话，用来保存当前登录用户的信息
			session.setAttribute("login", l);								//把获取的对象保存在 Session 中
			response.sendRedirect("main.jsp");										//验证成功返回字符串SUCCESS，此时 Session 中已经有用户对象
		}else{
			response.sendRedirect("error.jsp");											//验证失败返回字符串ERROR
		}
	}
	//属性 login 的 get/set 方法
	public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,
			IOException{
		doGet(request,response);
	}
	
}
